package com.lbssoft.datalink;

import com.lbssoft.datalink.entity.SyncDevice;
import com.lbssoft.datalink.entity.SyncGeofence;
import com.lbssoft.datalink.entity.SyncGroup;
import com.lbssoft.datalink.model.Result;
import com.lbssoft.datalink.sync.*;

import java.util.HashMap;
import java.util.Map;


/**
 * <b>实体对象管理器</b>
 * <p>用户管理业务系统与采集中心实体对象的同步维护</p>
 *
 * @author riven
 * @version 1.1.4
 */
public class EntityManager {
    GeofenceService geofenceImpl;
    GroupService groupImpl;
    DeviceService deviceImpl;
    PermisService permisImpl;
    CommandService commandImpl;

    public EntityManager(String appSecret) {
        this.geofenceImpl = new GeofenceService(appSecret);

        this.groupImpl = new GroupService(appSecret);

        this.deviceImpl = new DeviceService(appSecret);

        this.permisImpl = new PermisService(appSecret);

        this.commandImpl = new CommandService(appSecret);
    }

    /**
     * <b>创建围栏</b>
     *
     * @param geofence 围栏信息
     *                 <p>name(String),围栏名称，必填</p>
     *                 type(String),围栏类型，必填，目前有三种类型围栏  circle 圆形围栏、polygon 多边形、linestring 线路围栏容差范围100米<br>
     *                 area(String),围栏区域，必填，不同类型不同的数据格式<br><br>
     *                 圆形实例“31.769131936379196 117.26690883191068, 1811.3”<br>
     *                 多边形 “31.763261416599136 116.84671444671226, 31.727642345487084 116.82508511321616, 31.740490147335322 116.89580960052086, 31.763261416599136 116.84671444671226”<br>
     *                 线形状 “31.777530331174358 117.22496639295079, 31.75651131700264 117.22579264191606”<br>
     * @return Result
     * <p>{"code": 0,"msg": "OK","data": {"area": "31.769131936379196 117.26690883191068, 1811.3","name": "test","type": "circle","uuid": "87dc585ea587412ca6d44a2acb50f581"}}</p>
     * <p><font color=red>注意需要将获取到的UUID存入自己的业务系统</font></p>
     */
    public Result addGeoFence(SyncGeofence geofence) {
        com.lbssoft.datalink.model.Result res = geofenceImpl.add(geofence);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>删除围栏</b>
     *
     * @param uuid 围栏UUID
     * @return Result
     */
    public Result delGeoFence(String uuid) {
        com.lbssoft.datalink.model.Result res = geofenceImpl.delete(uuid);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>修改围栏</b>
     *
     * @param geofence 围栏信息<br>
     *                 uuid(String),围栏UUID，必填 ，采集中心围栏UUID，需要保存到业务系统中 用来同步管理围栏<br>
     *                 name(String)，围栏名称，必填<br>
     *                 type(String),围栏类型，必填,目前有三种类型围栏  circle 圆形围栏、polygon 多边形、linestring 线路围栏容差范围100米<br>
     *                 area(String),围栏区域，必填<br>
     * @return Result
     */
    public Result updateGeoFence(SyncGeofence geofence) {
        com.lbssoft.datalink.model.Result res = geofenceImpl.update(geofence);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>创建分组</b>
     *
     * @param group 分组<br><br>
     *              name(String) ,分组别名，必填，<br>
     *              parentUUID(String),父分组UUID,选填，不填表示自己就是顶级分组，<br>
     * @return Result
     * <p>{"code": 0,"msg": "OK","data": {"name": "test","uuid": "9b317da93f764b39b79c73954daad18c"}}</p>
     * <p><font color=red>注意需要将获取到的UUID存入自己的业务系统</font></p>
     */
    public Result addGroup(SyncGroup group) {
        com.lbssoft.datalink.model.Result res = groupImpl.add(group);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>修改分组</b>
     *
     * @param group 分组信息<br><br>
     *              uuid(String),分组UUID,必填,采集中心分组UUID<br>
     *              name(String),分组名称，必填<br>
     *              parentUUID(String),父分组UUID,选填 不传等于取消关联父分组<br>
     * @return Result
     * <p>{"code": 0,"msg": "OK","data": {"name": "test","uuid": "9b317da93f764b39b79c73954daad18c","parentUUID": "355bfe7fcc0a4716bcff93be43bc43e1"}}</p>
     */
    public Result updateGroup(SyncGroup group) {


        com.lbssoft.datalink.model.Result res = groupImpl.update(group);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>删除分组</b>
     *
     * @param uuid 分组uuid,必填，采集中心分组uuid
     * @return Result
     */
    public Result delGroup(String uuid) {
        com.lbssoft.datalink.model.Result res = groupImpl.delete(uuid);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>创建设备</b>
     *
     * @param device 设备信息<br><br>
     *               imei(String) 厂商设备唯一编号，必填 ，通常硬件设备上有标识<br>
     *               groupUUID(String) 分组uuid， 选填，  不设置表示不分组 否则表示所在分组uuid
     * @return Result
     * <p>
     *     {"code": 0,"msg": "创建成功","data": {"groupUUID": "355bfe7fcc0a4716bcff93be43bc43e1","name": "test","imei": "1000000013","uuid": "c0b2fdabdb914c029b9097e36de6eb82"}}
     * </p>
     * <p><font color=red>注意需要将获取到的UUID存入自己的业务系统</font></p>
     */
    public Result addDevice(SyncDevice device) {
//        if(device.getGroupUuid()==null){
//            device.setGroupId(0);
//        }
        com.lbssoft.datalink.model.Result res = deviceImpl.add(device);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * <b>修改设备</b>
     *
     * @param device 设备信息
     *               uuid(String),必填，设备UUID<br>
     *               imei(String),必填，设备IMEI<br>
     *               name(String),必填，设备别名<br>
     *               groupUUID(String),选填 不填等于取消设备分组，否则代表绑定分组
     * @return Result
     * <p>{"code": 0,"msg": "更新成功","data": {"uuid": "c0b2fdabdb914c029b9097e36de6eb82","name": "test","imei": "1000000013","groupUUID": "9b317da93f764b39b79c73954daad18c","speedLimit": null}}</p>
     */
    public Result updateDevice(SyncDevice device) {
        com.lbssoft.datalink.model.Result res = deviceImpl.update(device);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }


    /**
     * 删除设备
     *
     * @param uuid 设备uuid
     * @return Result
     */
    public Result delDevice(String uuid) {
        com.lbssoft.datalink.model.Result res = deviceImpl.delete(uuid);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

    /**
     * 权限关联
     *
     * @param param 参数 ,必填，{groupId=,geofenceId=} 或者{imei=,geofenceId=}<br>
     * @param link  true关联，false取消关联<br>
     * @return Result
     */
    public Result permission(Map<String, Object> param, boolean link) {
        Result res = new Result();
        if (link) {
            res = permisImpl.link(param);
        } else {
            res = permisImpl.unLink(param);
        }
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }


    /**
     * 命令下发
     *
     * @param deviceUUID       设备UUID号
     * @param type       命令类型
     * @param attributes 命令参数值
     * @return
     */
    public Result sendCommand(String deviceUUID, String type, Map attributes) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("deviceUUID", deviceUUID);
        map.put("type", type);
        map.put("attributes", attributes);
        Result res = commandImpl.send(map);
        if (res.getCode() == 200) {
            res.setCode(0);
        }
        return res;
    }

}
